package com.yoyo.yyadsdk.broad;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import androidx.appcompat.app.AppCompatActivity;

import com.yoyo.yyadsdk.activity.EmptyActivity;
import com.yoyo.yyadsdk.activity.LockActivity;
import com.yoyo.yyadsdk.activity.LockActivity_;
import com.yoyo.yyadsdk.bean.dao.AdvScreenEntity;
import com.yoyo.yyadsdk.config.PullFeature;
import com.yoyo.yyadsdk.util.AdvUtils;
import com.yoyo.yyadsdk.util.LogHelper;
import com.yoyo.yyadsdk.util.PullUtils;

public class ScreenStatusReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        LogHelper.e(intent.getAction());
        int actionType;
        String tPkg;
        switch (intent.getAction()) {
            case Intent.ACTION_SCREEN_ON:
                //判断上次拉活时间
                PullFeature.SCREEN_STATE = PullFeature.PULL_OPEN;
                if (System.currentTimeMillis() - PullFeature.getPullFeatureLastPullTime() > PullFeature.getPullFeatureInterval()) {
                    actionType = PullFeature.PULL_OPEN;
                    EmptyActivity.launch(context, actionType, PullUtils.getPullEntity(actionType));
                }
                if (isScreenOnLocked(context)) {
//                    AdvScreenEntity advScreenEntity = new AdvScreenEntity();
//                    advScreenEntity.setType(PullFeature.ADV_LOCK_SCREEN);
//                    advScreenEntity.setRuleId(0);
//                    advScreenEntity.setFrequency(10);
//                    advScreenEntity.setId(1);
//                    advScreenEntity.setStatus(PullFeature.USE_STATUS_ENABLE);
//                    advScreenEntity.setInterval(1);
//                    advScreenEntity.setActivities("com.dollkey.waveview.ClipChildrenActivity");
//                    advScreenEntity.setDuration(5);
//                    advScreenEntity.setPriority(1);
//                    LockActivity_.launch(context, advScreenEntity);
                    LockActivity_.launch(context, AdvUtils.getLockScreen(PullFeature.ADV_LOCK_SCREEN));
                    PullFeature.SCREEN_STATE_ = PullFeature.SCREEN_STATE_LOCK;
                } else {
                    PullFeature.SCREEN_STATE_ = PullFeature.SCREEN_STATE_UNLOCK;
                }
                break;
            case Intent.ACTION_SCREEN_OFF:
                PullFeature.SCREEN_STATE = PullFeature.PULL_CLOSE;
                PullFeature.SCREEN_STATE_ = PullFeature.SCREEN_STATE_LOCK;
                closeWindow(context, "close");
                if (System.currentTimeMillis() - PullFeature.getPullFeatureLastPullTime() > PullFeature.getPullFeatureInterval()) {
                    actionType = PullFeature.PULL_CLOSE;
                    EmptyActivity.launch(context, actionType, PullUtils.getPullEntity(actionType));
                }
                break;
            case Intent.ACTION_USER_PRESENT:
                PullFeature.SCREEN_STATE = PullFeature.PULL_UNLOCK;
                PullFeature.SCREEN_STATE_ = PullFeature.SCREEN_STATE_UNLOCK;
                if (System.currentTimeMillis() - PullFeature.getPullFeatureLastPullTime() > PullFeature.getPullFeatureInterval()) {
                    actionType = PullFeature.PULL_UNLOCK;
                    EmptyActivity.launch(context, actionType, PullUtils.getPullEntity(actionType));
                }
                break;
            default:
                break;
        }
    }

    // 判断是否亮屏锁屏状态
    public static boolean isScreenOnLocked(Context context) {
        if (isScreenOn(context)) {
            KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(AppCompatActivity.KEYGUARD_SERVICE);
            return mKeyguardManager.inKeyguardRestrictedInputMode();
        } else {
            return false;
        }
    }

    public static boolean isScreenUnLocked(Context context) {
        if (isScreenOn(context)) {
            KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(AppCompatActivity.KEYGUARD_SERVICE);
            return !mKeyguardManager.inKeyguardRestrictedInputMode();
        } else {
            return false;
        }
    }

    private static boolean isScreenOn(Context context) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }


    private void closeWindow(Context context, String runningActivity) {
        Intent intent = new Intent(DismissReceiver.ACTION);
        intent.putExtra("aty", runningActivity);
        context.sendBroadcast(intent);
    }
}